-- Autocmds are automatically loaded on the VeryLazy event
-- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
-- Add any additional autocmds here

local function augroup(name)
    return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true})
end

vim.api.nvim_create_autocmd({"BufWritePre"}, {
    group = augroup("del_trail_spaces"),
    pattern = {"*.*"},
    -- orignal cmd is ":$s/\s\+$//e"
    command = ":%s/\\s\\+$//e",

    -- another method to delete trail white spaces.
    -- callback = function ()
    --     vim.cmd(":%s/\\s\\+$//e")
    -- end
})

vim.api.nvim_create_autocmd({"InsertLeave", "TextChanged"}, {
    pattern = {"*"},
    callback = function()
        local disable_file_types = {
            "",
            "toggleterm",
            "translate",
        }
        local directory = vim.fn.fnamemodify(vim.fn.expand("%"), ":p:h")
        if vim.fn.isdirectory(directory) == 0 and not vim.tbl_contains(disable_file_types, vim.bo.filetype) then
            vim.fn.mkdir(directory, "p")
        end
        vim.cmd("silent! wall")
    end,
    -- command = "silent! wall",
    nested = true,
})
